package com.vectorx.myDesignDemos.src.main.java.单例_双重校验;

public class Demo {

    public static void main(String[] args) {
        DemoObj instance1 = DemoObj.getInstance();
        DemoObj instance2 = DemoObj.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode());
        System.out.println(instance2.hashCode());
    }
}


class DemoObj {

    //构造方法私有化
    private DemoObj() {
    }

    //声明类实例
    private static volatile DemoObj demoObj;

    //提供静态公共方法获取实例
    public static DemoObj getInstance() {
        if (demoObj == null) {
            synchronized (DemoObj.class) {
                if (demoObj == null) {
                    demoObj = new DemoObj();
                }
            }
        }
        return demoObj;
    }
}
